package com.example.mypro;

import com.example.domain.XueSheng;
import com.example.factory.ServiceFactory;
import com.example.mypro.R;
import com.example.service.XueShengService;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

public class LoginActivity extends Activity implements OnClickListener  {
	private EditText etname;
	private EditText etpassword;
    private Intent intent;
	private Button login;
	private ProgressBar pb;
	private Button reset;
    private Button regist;
    private XueShengService xueShengService;
    private XueSheng xueSheng=new XueSheng();
    private GetData getData;
    private String xh;
//    private Boolean flag=false;
    
	protected void onCreate(Bundle savedInstanceState) {
		xueShengService=ServiceFactory.getShengService();
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_loging);
		etname =(EditText)findViewById(R.id.login_et_user);
		etpassword =(EditText)findViewById(R.id.login_et_paw);
		login = (Button)findViewById(R.id.bt_login);
		reset =(Button)findViewById(R.id.bt_reset);
		regist =(Button)findViewById(R.id.bt_registration);
		pb=(ProgressBar)findViewById(R.id.login_pb);
		pb.setVisibility(View.INVISIBLE);
		login.setOnClickListener(this);
		reset.setOnClickListener(this);
		regist.setOnClickListener(this);
}
	@SuppressLint("ShowToast")
	class GetData extends AsyncTask<Void, Void, XueSheng> {

		@SuppressLint("ShowToast")
		@Override
		protected XueSheng doInBackground(Void... params) {
			System.out.println("doInBackground");			
				xueSheng=xueShengService.login(etname.getText().toString(), etpassword.getText().toString());
			return xueSheng;
		}

		@Override
		protected void onProgressUpdate(Void... values) {
			super.onProgressUpdate(values);

		}

		protected void onPostExecute(XueSheng result) {
			super.onPostExecute(result);
			// System.out.println("onProgressUpdate");
		pb.setVisibility(View.VISIBLE);
				if(null!=xueSheng)
					{
					xh =String.valueOf(xueSheng.getXs_xh());
					System.out.println(xh);
					pb.setVisibility(View.INVISIBLE);
					intent = new Intent(LoginActivity.this, BugaolanActivity.class);
					intent.putExtra("xs_dh", xueSheng.getXs_dh());
					intent.putExtra("xs_xh",xh);
					intent.putExtra("xs_xm", xueSheng.getXs_xm());
					startActivity(intent);
			}
				else {
		            Toast.makeText(LoginActivity.this, "登陆失败", 1).show();
		            
				}
		}

	}
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bt_reset:
			etname.setText("");
			etpassword.setText("");
			break;
		case R.id.bt_login:				
				getData = new GetData();
				getData.execute();
				pb.setVisibility(View.INVISIBLE);
				
			break;
		case R.id.bt_registration:
			intent = new Intent(LoginActivity.this, ZhuCeActivity.class);
			startActivity(intent);
			break;
			}
	}
	}